﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using WoSoft.PACS.Win.MyToolBars;
using WoSoft.PACS.Win.Forms.SubForms;
using WoSoft.PACS.Data.TemplateData;

namespace WoSoft.PACS.Win.Forms
{
    /// <summary>
    /// 原型管理
    /// </summary>
    public partial class FrmArchetype : FrmBase
    {

        private TemplateDataService _templateDataService = new TemplateDataService();
        public FrmArchetype()
        {
            InitializeComponent();
        }

        public FrmArchetype(RibbonBar ribar)
            : base(ribar)
        {
            InitializeComponent();
        }

        public override List<MyToolBars.OperationModel> Operations()
        {
            List<OperationModel> functionInfos = new List<OperationModel>();

            //创建原型
            var createButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "创建报告模块",
                Image = global::WoSoft.PACS.Win.Properties.Resources.add2,
                ClickEvent = new EventHandler(CreateTemplateClick),
            };
            functionInfos.Add(createButton);

            //修改原型
            //var editButton = new OperationModel()
            //{
            //    ID = Guid.NewGuid().ToString(),
            //    Name = "修改报告模块",
            //    Image = global::WoSoft.PACS.Win.Properties.Resources.document_edit,
            //    ClickEvent = new EventHandler(EditPatientClick),
            //};
            //functionInfos.Add(editButton);

            //删除原型
            var deleteButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "删除报告模块",
                Image = global::WoSoft.PACS.Win.Properties.Resources.document_delete,
                ClickEvent = new EventHandler(DeletePatientClick),
            };
            functionInfos.Add(deleteButton);

            //设置为默认原型
            var setDefaultButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "设置为启用",
                Image = global::WoSoft.PACS.Win.Properties.Resources.check2,
                ClickEvent = new EventHandler(SetDefaultClick),
            };
            functionInfos.Add(setDefaultButton);


            //刷新原型
            var refreshButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "刷新报告模块",
                Image = global::WoSoft.PACS.Win.Properties.Resources.refresh,
                //ClickEvent = new EventHandler(RefreshClick),
            };
            functionInfos.Add(refreshButton);
            return functionInfos;
        }

        //创建模板
        private void CreateTemplateClick(object sender, EventArgs e)
        {
            FrmArcheTypeInfo frmArcheType = new FrmArcheTypeInfo(string.Empty);
            var r = frmArcheType.ShowDialog(this);
            if (r == System.Windows.Forms.DialogResult.OK)
            {//刷新
                RefreshData();
            }
        }

        //删除报告模块
        private void DeletePatientClick(object sender, EventArgs e)
        {
            if (this.dataGridViewX1.SelectedRows.Count <= 0)
            {
                return;
            }

            var id = this.dataGridViewX1.SelectedRows[0].Cells["colID"].Value.ToString();
            if (!string.IsNullOrEmpty(id))
            {
                //确定要删除
                if (MessageBoxEx.Show("您确定要删除该报告模版吗？", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    _templateDataService.DeleteDefault(id);
                    RefreshData();
                }
            }
        }
        //修改报告模块
        private void EditPatientClick(object sender, EventArgs e)
        {
        }
        //设置为使用状态
        private void SetDefaultClick(object sender, EventArgs e)
        {
            if (this.dataGridViewX1.SelectedRows.Count <= 0)
            {
                return;
            }

            var id = this.dataGridViewX1.SelectedRows[0].Cells["colID"].Value.ToString();
            var type = this.dataGridViewX1.SelectedRows[0].Cells["colType"].Value.ToString();
            if (!string.IsNullOrEmpty(id))
            {
                _templateDataService.UpdateDefault(id,type);
                RefreshData();
            }

        }

        private void RefreshData()
        {
            var dt = _templateDataService.GetAllData();
            this.dataGridViewX1.DataSource = dt;
        }

        //加载数据
        private void FrmArchetype_Load(object sender, EventArgs e)
        {
            RefreshData();
        }
    }
}
